<template>
    <div class="login_container">
        <!-- 登录盒子  -->
        <div class="login_box">
            <!-- 头像 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登录表单 -->
            <!--<el-form :model="loginForm" ref="LoginFormRef" :rules="loginFormRules" label-width="0px" class="login_form">
                &lt;!&ndash; 用户名 &ndash;&gt;
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" ></el-input>
                </el-form-item>
                &lt;!&ndash; 密码 &ndash;&gt;
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
                </el-form-item>
                &lt;!&ndash; 按钮 &ndash;&gt;
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>-->
            <el-form :model="loginUserInfo" :rules="rules" ref="LoginFormRef" label-width="100px" class="login_form">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginUserInfo.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="loginUserInfo.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="loginForm">登录</el-button>
                    <el-button type="success" @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>


        </div>
    </div>
</template>

<script>
    export default {
        data(){
            return {
                loginUserInfo: {
                    username: "admin",
                    password: "123456"
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            loginForm(){
                this.$refs.LoginFormRef.validate(async valid => {
                    //如果valid参数为true则验证通过
                    if (!valid) return;


                    let {data: res} = await this.$http.post("login",this.loginUserInfo);
                    console.log("res===>",res);

                    if(res.meta.status !== 200){
                        return this.$message.error("登录失败:"+res.meta.msg);
                    }

                    window.sessionStorage.setItem("token", res.data.token);

                    this.$router.push("/home")

                })
            },
            resetForm(){
                console.log("resetForm------");
                this.$refs.LoginFormRef.resetFields();
            },
        }
    }
</script>

<style lang="scss" scoped type="text/scss">
    .login_container {
        background-color: #2b5b6b;
        height: 100%;
    }

    .login_box {
        width: 450px;
        height: 300px;
        background: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {
            position: absolute;
            left: 50%;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            width: 130px;
            height: 130px;
            box-shadow: 0 0 10px #ddd;
            transform: translate(-50%, -50%);
            background-color: #fff;

            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }

    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }

    .btns {
        display: flex;
        justify-content: flex-end;
    }
</style>